//look into this:
//userSchema.methods.validPassword = function validPass(pass){ validate password code}
var mongoose = require('mongoose');
//Schema = mongoose.Schema;
//Model = mongoose.model;

exports.dbutil = function (req, res) {

    var pongSchema = mongoose.Schema({
        username: {type: String, required: true, index: {unique: true}},
        password: {type: String, required: true}

    });

    var User = mongoose.model('user', pongSchema);

    var currentUser = new User({username: req.body.username, password: req.body.password});
   // var postusername = req.body.username;
   //var postpassword = req.body.password;

    console.log("username: " + currentUser.username + " and password: " + currentUser.password);




    console.log("executing in dbutil");
    mongoose.connect('mongodb://localhost/pongdb');
    var db = mongoose.connection;

    //this should be made a declared function and called explicitly to make code cleaner
    db.on('error', console.error.bind(console, 'connection error'));
    db.once('open', function callback() {

        currentUser.save(function (err) {
            if (err) {
                console.log("error saving model: " + err);
            }
            console.log("user saved to database");
        });


    });

    var save = function (data) {
        data.save(function (err) {
            if (err) {
                console.log("error saving model: " + err);
            }
            console.log("user saved to database");
        });
    };

    var lookup = function (data) {
        data.find(function (err, results) {
            if (err) {
                console.log("encountered " + err + "during execution");
            }

            console.log("database contains " + results);
        });
    };


};


/*
 index.post('/login', function(req, res){
 new user({
 username : req.body.username,
 password : req.body.password
 }).save(function(err, doc){
 if(err) res.json(err);
 else    res.send('Succesfully inserted');
 });
 });
 */


/*
 var alex = new PongModel ({
 username: 'tiddly derp',
 password: 'constable'
 //admin: true
 });

 */
/*
 var UserSchema = new Schema({

 username: {type: String, required: true, index: {unique: true}},
 password: {type: String, required: true}

 });
 */





/*var mongoose = require ('mongoose');
 //Schema = mongoose.Schema;
 //Model = mongoose.model;

 function dbtest(req, res){
 var username = req.username;
 var password = req.password;
 console.log("yep doing stuff in dbtest");
 mongoose.connect('mongodb://localhost/pongdb');
 var db = mongoose.connection;

 db.on('error', console.error.bind(console, 'connection error'));
 db.once('open', function callback (){

 mongoose.model(post, pongSchema).save(function (err, post){
 if (err){
 console.log("error saving model: "+err);
 }
 });



 PongModel.find(function(err, pongs){
 if (err) {
 console.log("encountered " + err +"during execution");
 }

 console.log("database contains " +pongs);
 })
 });

 var pongSchema = mongoose.Schema({
 username: {type: String, required: true, index: {unique: true}},
 password: {type: String, required: true}

 });

 var user  = mongoose.model('user', pongSchema);

 };


 /*
 index.post('/login', function(req, res){
 new user({
 username : req.body.username,
 password : req.body.password
 }).save(function(err, doc){
 if(err) res.json(err);
 else    res.send('Succesfully inserted');
 });
 });
 */


/*
 var alex = new PongModel ({
 username: 'tiddly derp',
 password: 'constable'
 //admin: true
 });

 */
/*
 var UserSchema = new Schema({

 username: {type: String, required: true, index: {unique: true}},
 password: {type: String, required: true}

 });


 exports.dbtest = dbtest;

 // module.exports = mongoose.model(User, UserSchema); */